package com.nwpu.server.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nwpu.server.admin.dao.EmbeddingMethodDao;
import com.nwpu.server.admin.dao.EmbeddingmethodTitleDao;
import com.nwpu.server.admin.entity.EmbeddingMethodEntity;
import com.nwpu.server.admin.entity.EmbeddingmethodTitleEntity;
import com.nwpu.server.admin.entity.PipelineEntity;
import com.nwpu.server.admin.entity.PipelineTitleEntity;
import com.nwpu.server.admin.service.EmbeddingmethodTitleService;
import com.nwpu.server.common.response.PageUtils;
import com.nwpu.server.common.response.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("embeddingmethodTitleService")
public class EmbeddingmethodTitleServiceImpl extends ServiceImpl<EmbeddingmethodTitleDao, EmbeddingmethodTitleEntity> implements EmbeddingmethodTitleService {

    @Autowired
    private EmbeddingMethodDao embeddingMethodDao;

    @Autowired
    private EmbeddingmethodTitleDao embeddingmethodTitleDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EmbeddingmethodTitleEntity> page = this.page(
                new Query<EmbeddingmethodTitleEntity>().getPage(params),
                new QueryWrapper<EmbeddingmethodTitleEntity>()
        );

        return new PageUtils(page);
    }

    @Cacheable(value = {"admin:embeddingMethod"},key = "#root.method.name",sync = true)
    @Override
    public List<EmbeddingmethodTitleEntity> getAll() {
        List<EmbeddingmethodTitleEntity> all = baseMapper.selectList(null);
        for (EmbeddingmethodTitleEntity embeddingmethodTitleEntity : all) {
            Long id = embeddingmethodTitleEntity.getId();
            QueryWrapper<EmbeddingMethodEntity> title_id = new QueryWrapper<EmbeddingMethodEntity>().eq("title_id", id);
            List<EmbeddingMethodEntity> embeddingMethodEntities = embeddingMethodDao.selectList(title_id);
            embeddingmethodTitleEntity.setNames(embeddingMethodEntities.stream().parallel().map(EmbeddingMethodEntity::getSingleMethodName).collect(Collectors.toList()));
        }
        return all;
    }

}